package com.hg.library.utils;

import android.content.Context;
import android.os.Environment;
import android.util.*;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ASUS-PC on 2016/4/13.
 */
public class IoUtils {

    private IoUtils() {
    }

    /**
     * 数据写入本地文件中
     *
     * @param context
     * @param fileName
     * @param obj
     */
    public static boolean writeObjectToFile(Context context, String fileName, Object obj) {
        try {
            FileOutputStream stream = context.openFileOutput(EncryptUtils.castToMD5(fileName), context.MODE_PRIVATE);
            ObjectOutputStream outputStream = new ObjectOutputStream(stream);
            outputStream.writeObject(obj);
            outputStream.close();
            stream.flush();
            return true;
        } catch (Exception e) {
            context.deleteFile(EncryptUtils.castToMD5(fileName));
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 读取本地文件数据
     *
     * @param context
     * @param fileName
     * @return Object
     */
    public static <T> T readObjectFromFile(Context context, String fileName) {
        Object temp = null;
        try {
            FileInputStream stream = context.openFileInput(EncryptUtils.castToMD5(fileName));
            ObjectInputStream inputStream = new ObjectInputStream(stream);
            temp = inputStream.readObject();
            inputStream.close();
            stream.close();
        } catch (Exception e) {
            context.deleteFile(EncryptUtils.castToMD5(fileName));
            e.printStackTrace();
        }
        return (T) temp;
    }

    public static String toString(InputStream inputStream) {
        return toString(inputStream, false);
    }

    public static String toString(InputStream inputStream, boolean prettyFormat) {
        StringBuffer stringBuffer = null;
        String format = "";
        if (prettyFormat) format = "\n";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            stringBuffer = new StringBuffer();
            String s;
            while ((s = reader.readLine()) != null) {
                stringBuffer.append(s + format);
            }
            reader.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    public static void writeErrorReports(Context context, String content) {

        String path = Environment.getExternalStorageDirectory().getPath() + "/errorLog";

        File dir = new File(path);
        if (!dir.exists()) dir.mkdir();

        File file = new File(path, context.getApplicationInfo().loadLabel(context.getPackageManager()) + ".log");

        content = new SimpleDateFormat("yy-MM-dd hh:mm:ss").format(new Date()) + "\n" + content;

        try {
            file.createNewFile();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            bufferedOutputStream.write(content.getBytes());
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
